/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.WxUserRecommand;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.WxUserRecommandRepository;
import me.zhengjie.modules.system.service.WxUserRecommandService;
import me.zhengjie.modules.system.service.dto.WxUserRecommandDto;
import me.zhengjie.modules.system.service.dto.WxUserRecommandQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.WxUserRecommandMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wangyu
* @date 2021-11-25
**/
@Service
@RequiredArgsConstructor
public class WxUserRecommandServiceImpl implements WxUserRecommandService {

    private final WxUserRecommandRepository wxUserRecommandRepository;
    private final WxUserRecommandMapper wxUserRecommandMapper;

    @Override
    public Map<String,Object> queryAll(WxUserRecommandQueryCriteria criteria, Pageable pageable){
        Page<WxUserRecommand> page = wxUserRecommandRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(wxUserRecommandMapper::toDto));
    }

    @Override
    public List<WxUserRecommandDto> queryAll(WxUserRecommandQueryCriteria criteria){
        return wxUserRecommandMapper.toDto(wxUserRecommandRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public WxUserRecommandDto findById(Long wurId) {
        WxUserRecommand wxUserRecommand = wxUserRecommandRepository.findById(wurId).orElseGet(WxUserRecommand::new);
        ValidationUtil.isNull(wxUserRecommand.getWurId(),"WxUserRecommand","wurId",wurId);
        return wxUserRecommandMapper.toDto(wxUserRecommand);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WxUserRecommandDto create(WxUserRecommand resources) {
        return wxUserRecommandMapper.toDto(wxUserRecommandRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WxUserRecommand resources) {
        WxUserRecommand wxUserRecommand = wxUserRecommandRepository.findById(resources.getWurId()).orElseGet(WxUserRecommand::new);
        ValidationUtil.isNull( wxUserRecommand.getWurId(),"WxUserRecommand","id",resources.getWurId());
        wxUserRecommand.copy(resources);
        wxUserRecommandRepository.save(wxUserRecommand);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long wurId : ids) {
            wxUserRecommandRepository.deleteById(wurId);
        }
    }

    @Override
    public void download(List<WxUserRecommandDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WxUserRecommandDto wxUserRecommand : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("推荐人openId", wxUserRecommand.getOpenId());
            map.put("目标openId", wxUserRecommand.getObjOpenid());
            map.put("时间", wxUserRecommand.getCreateTime());
            map.put("昵称", wxUserRecommand.getNickName());
            map.put("头像", wxUserRecommand.getHeadPic());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public WxUserRecommand findOne(String openId, String targetOpenId) {
        WxUserRecommandQueryCriteria criteria = new WxUserRecommandQueryCriteria();
        //criteria.setOpenId(openId);
        criteria.setObjOpenid(targetOpenId);
        List<WxUserRecommand> all = wxUserRecommandRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        if (CollectionUtils.isEmpty(all)){
            return null;
        }
        return all.get(0);
    }
}